package pandabay2.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pandabay2.DAO.AuctionDAO;
import pandabay2.domain.Auction;
import pandabay2.domain.User;

/**
 * Servlet implementation class MyAuctionServlet
 */
public class MyAuctionBuyerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyAuctionBuyerServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// see if it is a logged-in user
		HttpSession session = request.getSession(true);
		User user = (User) session.getAttribute("currentSessionUser");
		if(user == null) {
			response.sendRedirect("LoginPage.jsp");
		}
		
		try {
			int user_id = user.getUser_id();
			System.out.println("In the My Auction Buyer Servlet");
			List<Auction> auctions;

			auctions = AuctionDAO.findIdBuyer(user_id);
			
			if(auctions.isEmpty()) request.setAttribute("message", "Keine Auktionen vorhanden");
			request.setAttribute("auctions", auctions);
			request.getRequestDispatcher("/MyAuctionsBuyer.jsp").forward(request, response);
		} catch (Throwable exc) {
			System.out.println(exc);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	}
}